<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>memoize.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_memoize_memoize.js.html">memoize.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Tool for caching the result of expensive deterministic
<a name="line17"></a> * functions.
<a name="line18"></a> *
<a name="line19"></a> * @see http://en.wikipedia.org/wiki/Memoization
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.memoize&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Decorator around functions that caches the inner function&#39;s return values.
<a name="line28"></a> * @param {Function} f The function to wrap. Its return value may only depend
<a name="line29"></a> *     on its arguments and &#39;this&#39; context. There may be further restrictions
<a name="line30"></a> *     on the arguments depending on the capabilities of the serializer used.
<a name="line31"></a> * @param {function(number, Object): string=} opt_serializer A function to
<a name="line32"></a> *     serialize f&#39;s arguments. It must have the same signature as
<a name="line33"></a> *     goog.memoize.simpleSerializer. It defaults to that function.
<a name="line34"></a> * @this {Object} The object whose function is being wrapped.
<a name="line35"></a> * @return {!Function} The wrapped function.
<a name="line36"></a> */
<a name="line37"></a>goog.memoize = function(f, opt_serializer) {
<a name="line38"></a>  var functionUid = goog.getUid(f);
<a name="line39"></a>  var serializer = opt_serializer || goog.memoize.simpleSerializer;
<a name="line40"></a>
<a name="line41"></a>  return function() {
<a name="line42"></a>    if (goog.memoize.ENABLE_MEMOIZE) {
<a name="line43"></a>      // In the strict mode, when this function is called as a global function,
<a name="line44"></a>      // the value of &#39;this&#39; is undefined instead of a global object. See:
<a name="line45"></a>      // https://developer.mozilla.org/en/JavaScript/Strict_mode
<a name="line46"></a>      var thisOrGlobal = this || goog.global;
<a name="line47"></a>      // Maps the serialized list of args to the corresponding return value.
<a name="line48"></a>      var cache = thisOrGlobal[goog.memoize.CACHE_PROPERTY_] ||
<a name="line49"></a>          (thisOrGlobal[goog.memoize.CACHE_PROPERTY_] = {});
<a name="line50"></a>      var key = serializer(functionUid, arguments);
<a name="line51"></a>      return cache.hasOwnProperty(key) ? cache[key] :
<a name="line52"></a>          (cache[key] = f.apply(this, arguments));
<a name="line53"></a>    } else {
<a name="line54"></a>      return f.apply(this, arguments);
<a name="line55"></a>    }
<a name="line56"></a>  };
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * @define {boolean} Flag to disable memoization in unit tests.
<a name="line62"></a> */
<a name="line63"></a>goog.memoize.ENABLE_MEMOIZE = true;
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * Clears the memoization cache on the given object.
<a name="line68"></a> * @param {Object} cacheOwner The owner of the cache. This is the {@code this}
<a name="line69"></a> *     context of the memoized function.
<a name="line70"></a> */
<a name="line71"></a>goog.memoize.clearCache = function(cacheOwner) {
<a name="line72"></a>  cacheOwner[goog.memoize.CACHE_PROPERTY_] = {};
<a name="line73"></a>};
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Name of the property used by goog.memoize as cache.
<a name="line78"></a> * @type {string}
<a name="line79"></a> * @private
<a name="line80"></a> */
<a name="line81"></a>goog.memoize.CACHE_PROPERTY_ = &#39;closure_memoize_cache_&#39;;
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Simple and fast argument serializer function for goog.memoize.
<a name="line86"></a> * Supports string, number, boolean, null and undefined arguments. Doesn&#39;t
<a name="line87"></a> * support \x0B characters in the strings.
<a name="line88"></a> * @param {number} functionUid Unique identifier of the function whose result
<a name="line89"></a> *     is cached.
<a name="line90"></a> * @param {Object} args The arguments that the function to memoize is called
<a name="line91"></a> *     with. Note: it is an array-like object, because supports indexing and
<a name="line92"></a> *     has the length property.
<a name="line93"></a> * @return {string} The list of arguments with type information concatenated
<a name="line94"></a> *     with the functionUid argument, serialized as \x0B-separated string.
<a name="line95"></a> */
<a name="line96"></a>goog.memoize.simpleSerializer = function(functionUid, args) {
<a name="line97"></a>  var context = [functionUid];
<a name="line98"></a>  for (var i = args.length - 1; i &gt;= 0; --i) {
<a name="line99"></a>    context.push(typeof args[i], args[i]);
<a name="line100"></a>  }
<a name="line101"></a>  return context.join(&#39;\x0B&#39;);
<a name="line102"></a>};
</pre>


</body>
</html>
